package com.wingice.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wingice.common.mybatis.tenant.TenantContextHolder;
import com.wingice.system.entity.*;
import com.wingice.system.service.*;
import com.wingice.spring.security.model.KnifeUser;
import com.wingice.spring.security.oauth.model.KnifeOAuthClientDetails;
import com.wingice.spring.security.oauth.service.KnifeOAuthClientDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 胡昊
 * Description:
 * Date: 2021/1/13
 * Time: 11:08
 * Mail: huhao9277@gmail.com
 */
@Service
public class KnifeOAuthClientDetailsServiceImpl implements KnifeOAuthClientDetailsService {

    @Resource
    private SysOauthClientDetailsService sysOauthClientDetailsService;
    @Resource
    private SysOauthClientResourceService sysOauthClientResourceService;
    @Resource
    private SysResourceService sysResourceService;

    @Override
    public KnifeUser clientCredentialsAuthorize(String clientId, String clientSecret) {
        final SysOauthClientDetails clientDetails = sysOauthClientDetailsService.getOne(Wrappers.<SysOauthClientDetails>lambdaQuery()
                .eq(SysOauthClientDetails::getClientId, clientId)
                .eq(SysOauthClientDetails::getClientSecret, clientSecret));
        if (clientDetails == null) {
            return null;
        }
        final KnifeUser knifeUser = new KnifeUser();
        knifeUser.setTenantId(TenantContextHolder.getTenantId());
        knifeUser.setUsername(clientDetails.getClientId());
        knifeUser.setName(clientDetails.getClientName());
        final Set<String> resourceIdSet = sysOauthClientResourceService.list(Wrappers.<SysOauthClientResource>lambdaQuery()
                        .select(SysOauthClientResource::getResourceId)
                        .eq(SysOauthClientResource::getOauthClientId, clientDetails.getId()))
                .stream()
                .map(SysOauthClientResource::getResourceId)
                .collect(Collectors.toSet());
        if (!CollectionUtils.isEmpty(resourceIdSet)) {
            final List<SysResource> sysResourceList = sysResourceService.list(Wrappers.<SysResource>lambdaQuery()
                    .in(SysResource::getId, resourceIdSet));
            knifeUser.setPermissionList(sysResourceList.stream().map(SysResource::getPermission).collect(Collectors.toList()));
            knifeUser.setSysResourceList(sysResourceList);
        }
        return knifeUser;
    }

    @Override
    public KnifeOAuthClientDetails getKnifeOauthClientDetails(String clientId) {
        final SysOauthClientDetails clientDetails = sysOauthClientDetailsService.getOne(Wrappers.<SysOauthClientDetails>lambdaQuery().eq(SysOauthClientDetails::getClientId, clientId));
        if (clientDetails != null) {
            final KnifeOAuthClientDetails aceOAuthClientDetails = new KnifeOAuthClientDetails();
            BeanUtils.copyProperties(clientDetails, aceOAuthClientDetails);
            return aceOAuthClientDetails;
        }
        return null;
    }
}
